package com.example.tool.erp.demo;

import com.example.tool.erp.sdk.Client;
import com.example.tool.erp.sdk.Pager;
import com.example.tool.erp.sdk.WdtErpException;
import com.example.tool.erp.sdk.api.aftersales.RefundAPI;
import com.example.tool.erp.sdk.api.aftersales.dto.RefundSearchHistoryRequest;
import com.example.tool.erp.sdk.api.aftersales.dto.RefundSearchHistoryResponse;
import com.example.tool.erp.sdk.api.aftersales.dto.RefundSearchRequest;
import com.example.tool.erp.sdk.api.aftersales.dto.RefundSearchResponse;
import com.example.tool.erp.sdk.impl.ApiFactory;
import com.example.tool.erp.sdk.impl.DefaultClient;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

public class Refund
{
	public static void main(String[] args) throws IOException, WdtErpException
	{
		Client client = DefaultClient.get("wdterp30", "http://172.172.1.62:30000/", "xyx_api",
				"e8866c1681dacc9488a19c89991791b8:8f5800dab84b7a8d614e92cba739032c");
		RefundAPI refundApi = ApiFactory.get(client, RefundAPI.class);

		testSearchHistoryRefund(refundApi);
	}

	private static void testSearchRefund(RefundAPI refundApi)
	{
		RefundSearchRequest request = new RefundSearchRequest();
		request.setShopNos("POS");
		request.setModifiedFrom("2019-09-12 11:00");
		request.setModifiedTo("2019-11-24 11:26:31");

		RefundSearchResponse response = refundApi.search(request, new Pager(10, 0, true));

		if (response == null || response.getTotal() <= 0)
		{
			System.out.println("total_count: " + response.getTotal());
			return;
		}
		for (RefundSearchResponse.RefundOrderDto order : response.getOrders())
		{
			System.out.println("refund_no: " + order.getRefundNo());
			if (order.getType().equals(RefundSearchResponse.RefundOrderDto.REFUND_TYPE_SWAP_GOODS))
			{
				List<String> swapMerchantNos = order.getSwapOutDetailList().parallelStream()
						.map(RefundSearchResponse.SwapOrderInfoDto::getMerchantNo).collect(Collectors.toList());
				System.out.print(" swap order trade_no: " + order.getSwapOutTradeNo() + " merhant_nos:"
						+ StringUtils.join(swapMerchantNos, ","));
			}
			List<String> refundOrderDetailSpecNos = order.getDetailList().parallelStream()
					.map(RefundSearchResponse.RefundOrderInfoDto::getSpecNo).collect(Collectors.toList());
			System.out.println("refund_detail spec_nos: " + StringUtils.join(refundOrderDetailSpecNos, ","));
		}
		System.out.println(
				"total :" + response.getTotal() + " first refund_no: " + response.getOrders().get(0).getRefundNo());
	}

	private static void testSearchHistoryRefund(RefundAPI refundApi) throws WdtErpException
	{
		RefundSearchHistoryRequest request = new RefundSearchHistoryRequest();
		request.setStartTime("2022-01-01");
		request.setEndTime("2022-01-29");

		//		RefundSearchResponse response = refundApi.search(request, new Pager(10, 0, true));
		RefundSearchHistoryResponse response = refundApi.searchHistory(request, new Pager(10, 0, true));

		if (response == null || response.getTotalCount() <= 0)
		{
			System.out.println("total_count: " + response.getTotalCount());
			return;
		}
		for (RefundSearchHistoryResponse.Order order : response.getOrders())
		{
			System.out.println("refund_no: " + order.getRefundNo());
			List<String> refundOrderDetailSpecNos = order.getDetailList().parallelStream()
					.map(RefundSearchHistoryResponse.Order.Detail::getSpecNo).collect(Collectors.toList());
			System.out.println("refund_detail spec_nos: " + StringUtils.join(refundOrderDetailSpecNos, ","));
		}
		System.out.println(
				"total :" + response.getTotalCount() + " first refund_no: " + response.getOrders().get(0).getRefundNo());
	}
}
